﻿@{
    ViewBag.Title = "Home";
}

<h2>Home</h2>
<script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js"></script>
<form>
    <textarea id="showMessages" rows="5" cols="100"></textarea>
    <input id="messages" type="text" />
    <input type="button" id="send" value="Send msg" />
     <input type="button" id="get" value="Get msg" />
    <script type="text/javascript">

        $("#send").click(function (e) {
            var txt = $("#messages").val();
            $.ajax({
                url: 'http://chatwebserver/api/MessageWebApi/Chat',
                type: 'POST',
                data: '{"Text":"' + txt + '","Date":"' + new Date().toISOString() + '","ChatTopicGuidId":"0AA7C33A-6EB7-40C0-B254-6542792FD2D7","UserGuidId":"29FBE327-638E-41D6-9251-C83D4C969F4A"}',
                async: false,
                cache: false,
                contentType: 'application/json'
            });
            return false;
        });

        $("#get").click(function () {

            var myDate = new Date();
            myDate.setMinutes(myDate.getMinutes() - 5);

            $.ajax({
                url: 'http://chatwebserver/api/MessageWebApi/GetMessage',
                type: 'GET',
                data: 'userId=0AA7C33A-6EB7-40C0-B254-6542792FD2D7&topicId=29FBE327-638E-41D6-9251-C83D4C969F4A&fromTime=' + encodeURI(myDate.toISOString()),
                success: function (data) {
                    var str = "";
                    for (var i = 0; i< data.length; i++)
                    {
                        str += data[i].Text + '\n';
                    }
                    $("#showMessages").val($("#showMessages").val() + str);
                },
                error: function () {
                }
            });
        });
        </script>
  
</form>

